using System;
using Oracle.ManagedDataAccess.Client;
using OracleInternal.Common;

namespace OracleInternal.TTC
{
	internal class TTCLobData : TTCMessage
	{
		internal const int LOBD_STATE0 = 0;

		internal const int LOBD_STATE1 = 1;

		internal const int LOBD_STATE2 = 2;

		internal const int LOBD_STATE3 = 3;

		internal const int LOBD_STATE_EXIT = 4;

		internal const short TTCG_LNG = 254;

		internal const short LOBDATALENGTH = 252;

		internal TTCLobData(MarshallingEngine mEngine)
			: base(mEngine, 14)
		{
		}

		internal void WriteLobData(byte[] inBuffer, long inBufferOffset, long numBytes)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131328, OracleTraceClassName.TTCLobData, OracleTraceFuncName.WriteLobData);
			}
			try
			{
				WriteTTCCode();
				m_marshallingEngine.MarshalCLR(inBuffer, (int)inBufferOffset, (int)numBytes);
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268566528, OracleTraceClassName.TTCLobData, OracleTraceFuncName.WriteLobData, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131584, OracleTraceClassName.TTCLobData, OracleTraceFuncName.WriteLobData);
				}
			}
		}

		internal long ReadLobData(byte[] outBuffer, long outBufferOffset)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131328, OracleTraceClassName.TTCLobData, OracleTraceFuncName.ReadLobData);
			}
			long num = 0L;
			int num2 = 0;
			long num3 = outBufferOffset;
			try
			{
				int num4 = 0;
				while (true)
				{
					switch (num4)
					{
					case 0:
						num2 = m_marshallingEngine.UnmarshalUB1();
						num4 = ((num2 != 254) ? 1 : 2);
						break;
					case 1:
						m_marshallingEngine.GetNBytes(outBuffer, (int)num3, num2);
						num += num2;
						num4 = 4;
						break;
					case 2:
						num2 = ((!m_marshallingEngine.m_bUseBigCLRChunks) ? m_marshallingEngine.UnmarshalUB1() : m_marshallingEngine.UnmarshalSB4());
						num4 = ((num2 <= 0) ? 4 : 3);
						break;
					case 3:
						m_marshallingEngine.GetNBytes(outBuffer, (int)num3, num2);
						num += num2;
						num3 += num2;
						num4 = 2;
						break;
					case 4:
						return num;
					}
				}
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268566528, OracleTraceClassName.TTCLobData, OracleTraceFuncName.ReadLobData, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131584, OracleTraceClassName.TTCLobData, OracleTraceFuncName.ReadLobData);
				}
			}
		}

		internal long ReadLobDataForArray()
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131328, OracleTraceClassName.TTCLobData, OracleTraceFuncName.ReadLobDataForArray);
			}
			long num = 0L;
			int num2 = 0;
			try
			{
				int num3 = 0;
				while (true)
				{
					switch (num3)
					{
					case 0:
						num2 = m_marshallingEngine.UnmarshalUB1();
						num3 = ((num2 != 254) ? 1 : 2);
						break;
					case 1:
						m_marshallingEngine.GetNBytes_ScanOnly(num2);
						num += num2;
						num3 = 4;
						break;
					case 2:
						num2 = ((!m_marshallingEngine.m_bUseBigCLRChunks) ? m_marshallingEngine.UnmarshalUB1() : m_marshallingEngine.UnmarshalSB4());
						num3 = ((num2 <= 0) ? 4 : 3);
						break;
					case 3:
						m_marshallingEngine.GetNBytes_ScanOnly(num2);
						num += num2;
						num3 = 2;
						break;
					case 4:
						return num;
					}
				}
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268566528, OracleTraceClassName.TTCLobData, OracleTraceFuncName.ReadLobDataForArray, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131584, OracleTraceClassName.TTCLobData, OracleTraceFuncName.ReadLobDataForArray);
				}
			}
		}
	}
}
